import {getToken,setToken,removeToken} from "@/utils/auth"
import {login,getUserInfo} from '@/api/user'
import {constantRoutes} from '@/router'
import {resetRouter} from '@/router'
const state={
  token:getToken(),
  // 从缓存中读取初始值
  userInfo: {}, // 存储用户基本资料状态
  routes:  constantRoutes    //静态路由的数组

}
const mutations={
  setToken(state,token){
      state.token =token
      // 同步到缓存
      setToken(token)
  },
  removeToken(state) {
    // 删除Vuex的token
    state.token =null
    removeToken()
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  setRoutes(state,newRoutes) {
    state.routes = [...constantRoutes,...newRoutes]     //静态路由＋动态路由
  }
}

const actions={
  async login(context,data ) {
    const token = await login(data)
    // todo:调用登录接口
    // 返回一个token 123456
    context.commit("setToken",token)
  },
  // 获取用户的基本资料
  async getUserInfo(context) {
    const result = await getUserInfo()
    context.commit('setUserInfo', result)
    return result
  },
  // 退出登录的action
  logout(context) {
    context.commit('removeToken') //删除token
    context.commit('setUserInfo',{}) //设置用户信息为空对象
    // 重置路由的方法
    resetRouter()
  }
}

export default {
  namespaced:true, //开启命名空间
  state,
  mutations,
  actions
}